#ifndef CONTROLLER_H
#define CONTROLLER_H

#include "CppInterface/Serial.h"
#include "CppInterface/UdpClient.h"
#include "CppInterface/DataProcessor.h"
#include "Config.h"
#include <QTimer>
#include <QStringDecoder>


class Controller : public QObject {
Q_OBJECT

public:
    explicit Controller(QObject *parent = nullptr);
    ~Controller() override;
    void setConfig(Config *cfg);
    // ===================== 通用参数设置 =====================
    void ReceiveData(const QByteArray &data) ;     //接收触发数据
    void SendMessage(QString &data);     //发送触发数据


    // ===================== 串口参数设置 =====================
    Q_INVOKABLE void openSerial();          //打开串口
    Q_INVOKABLE void closeSerial(); //关闭串口
    Q_INVOKABLE void UptateBaudRate(const QString &Str) ;//更新一下波特率
    Q_INVOKABLE QStringList scanSerialPorts() const;//获取串口列表
    Q_INVOKABLE QString currentPortDescription() const; //获取当前端口描述
    Q_INVOKABLE void setPortName(const QString &name);  // 设置当前串口名称
    Q_INVOKABLE void sendString(QString str) ;  //发送字符串
    Q_INVOKABLE void sendHex(QString str);      //发送16进制字符串

    // ===================== UDP参数设置 =====================
    Q_INVOKABLE void openUdp(); //打开UDP
    Q_INVOKABLE void closeUdp(); //关闭UDP

private:
    QString portName{};
    QString UptateBaudRateData{};
public slots:
    void onTimeout();   //定时器

signals:
    void receiveText(QString Data);   // 发给 QML 的信号
    void sendText(QString Data);   // 发给 QML 的信号
private:
SERIAL *Serial = nullptr;
Config *ConfigMode = nullptr;
QTimer *timer = nullptr;
UdpBackendClient *udpClient = nullptr;
DataProcessor *processor = nullptr;
};

#endif //CONTROLLER_H
